package com.example.doctor.mapper;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.example.common.entity.BaseInfo;
import com.example.common.entity.XDrugBaseInfo;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

public interface RxOrderMapper {
    @SelectProvider(type = SqlBuilder.class, method = "RxListSQL")
	@Results(id = "RxListResult", value = { 
            @Result(property = "RxOrderNO", column = "rx_order_no_nvarchar", id = true),
			@Result(property = "OrderNO", column = "order_no_nvarchar"),
			@Result(property = "Name", column = "name_nvarchar"),
			@Result(property = "Sex", column = "sex_nvarchar"),
			@Result(property = "Age", column = "age_int"),
			@Result(property = "CreateTime", column = "create_time_datetime"),
			@Result(property = "Dialectical", column = "dialectical_nvarchar"),
			@Result(property = "zDialectical", column = "z_dialectical_nvarchar"),
			@Result(property = "zDialectics", column = "z_dialectics_nvarchar"),
			@Result(property = "DrugInfo", column = "drug_info_nvarchar"),
			@Result(property = "ChkStatus", column = "chk_status_tinyint"),
			@Result(property = "FailureReason", column = "failure_reason_nvarchar"),
			@Result(property = "FailureRemark", column = "failure_remark_nvarchar"),
			@Result(property = "DType", column = "d_type_tinyint"), })
	List<Map<String, Object>> RxList(
        @Param("UID") long UID, @Param("Pid") long Pid, @Param("DType") String DType, @Param("skip") int skip, @Param("limit") int limit);

        @SelectProvider(type = SqlBuilder.class, method = "RxDrugListSQL")
	@Results(id = "RxDrugListResult", value = { 
            @Result(property = "RxOrderNO", column = "rx_order_no_nvarchar"),
			@Result(property = "DrugName", column = "drug_name_nvarchar"),
			@Result(property = "Spec", column = "spec_nvarchar"),
			@Result(property = "Num", column = "num_int"), })
        List<Map<String, Object>> RxDrugList(@Param("RxOrderNOs") String RxOrderNOs);

        @Select("select b.open_id_nvarchar as OpenID,a.oc_id_bigint as OCID,c.name as Name,b.phone_nvarchar as Phone,d.name_nvarchar as pName,d.id_no_nvarchar as idNo"
        +" from order_main a"
        +" inner join client_base_info b on a.client_id_bigint = b.uid_bigint"
        +" inner join doctor_base_info c on a.dc_id_bigint = c.dc_id"
        +" inner join client_patients_info d on a.pid_bigint = d.pid_bigint"
        +" where a.order_no_nvarchar = #{OrderNO}")
    Map<String, Object> OrderClientInfo(@Param("OrderNO") String OrderNO);

    @Select("select drug_id_bigint as DrugID,price_money as Price from z_drug_base_info where drug_id_bigint in (${DrugIDs})")
    List<Map<String, Object>> zBaseInfo(@Param("DrugIDs") String DrugIDs);
    @Select("select drug_id_bigint as DrugID,price_money as Price from x_drug_base_info where drug_id_bigint in (${DrugIDs})")
    List<Map<String, Object>> xBaseInfo(@Param("DrugIDs") String DrugIDs);

    @Select("select a.rx_order_no_nvarchar as RxOrderNO,c.name_nvarchar as Name,c.sex_nvarchar as Sex,c.age_int as Age,"
    +" e.name_nvarchar as depName,a.create_time_datetime as CreateTime"
    +" from order_rx_main a"
    +" inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
    +" inner join order_patients_info c on b.order_no_nvarchar = c.order_no_nvarchar"
    +" inner join doctor_base_info d on b.dc_id_bigint = d.dc_id"
    +" inner join h_hospital_department e on d.depart_id = e.id_bigint"
    +" where a.rx_order_no_nvarchar = #{RxOrderNO}"
    +" limit 0,1")
    Map<String, Object> PatientsInfo(@Param("RxOrderNO") String RxOrderNO);

    @Select("with paget as ("
    +" select order_no_nvarchar from order_main where dc_id_bigint = #{UID} and system_status_tinyint = 2"
    +" order by create_time_datetime desc"
    +" limit #{skip},#{limit})"
    +" select a.order_no_nvarchar as OrderNO,c.oc_id_bigint as OCID,a.create_time_datetime as CreateTime,a.ask_money_money as AskMoney,"
    +" b.name_nvarchar as Name,b.sex_nvarchar as Sex,b.age_int as Age,c.create_time_datetime as imbeg,c.end_time_datetime as EndTime,"
    +" c.oc_status_tinyint as OCStatus,isnull(d.order_no_nvarchar)  as IsComment"
    +" from order_main a"
    +" inner join paget e on a.order_no_nvarchar = e.order_no_nvarchar"
    +" inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
    +" inner join im_online_consultation c on a.order_no_nvarchar = c.order_no_nvarchar"
    +" left join order_comment d on a.order_no_nvarchar = d.order_no_nvarchar")
    List<Map<String, Object>> IMOrderList(@Param("UID") long UID, @Param("skip") int skip, @Param("limit") int limit);

    @Select("select drug_info_nvarchar as DrugInfo,does_int as Does,day_count_int as DayCount,avoid_nvarchar as Avoid,use_time_nvarchar as UseTime,cook_nvarchar as Cook,total_does_int as TotalDoes,med_way_nvarchar as MedWay"
    +" from order_rx_info where rx_order_no_nvarchar = #{RxOrderNO}")
    List<Map<String, Object>> selectzRxInfoList(@Param("RxOrderNO") String RxOrderNO);

    @Select("select drug_name_nvarchar as DrugName, spec_nvarchar as Spec, num_int as Num, CONCAT(med_way_nvarchar , \"；每日\" , freq_nvarchar , \"；\" , does_int , does_unit_nvarchar , \"/次；\" , day_int , \"天\") as Suggest, remark_nvarchar as Remark"
    +" from order_rx_drugs where rx_order_no_nvarchar = #{RxOrderNO}")
    List<Map<String, Object>> selectxRxInfoList(@Param("RxOrderNO") String RxOrderNO);

    @Select("select a.dc_id_bigint, a.receive_time_datetime, a.order_no_nvarchar, b.allergy_nvarchar, b.past_disease_nvarchar from order_main a"
    +" inner join order_patients_info b on a.patients_info_id_bigint = b.id_bigint"
    +" where a.system_status_tinyint = 2 and a.oc_status_tinyint != 0 and b.pid_bigint = #{pid}"
    +" order by a.create_time_datetime desc"
    +" limit 1")
    Map<String, Object> selectLastOrder(@Param("pid") long pid);

    @SelectProvider(type = SqlBuilder.class, method = "PcRxListSQL")
    List<Map<String, Object>> PcRxList(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType, 
    @Param("chkStatus") int chkStatus, @Param("name") String name, @Param("skip") int skip, @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "PcRxCountSQL")
    int PcRxCount(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType, 
    @Param("chkStatus") int chkStatus, @Param("name") String name);


    @Select("select CONCAT(code_nvarchar , ' ' , disease_name_nvarchar) from h_disease_info where id_bigint in (#{DisIDS})")
    List<String> findDisease(@Param("DisIDS") String DisIDS);

        class SqlBuilder{
            public String RxListSQL(
                @Param("UID") long UID, @Param("Pid") long Pid, @Param("DType") String DType, @Param("skip") int skip, @Param("limit") int limit) {
                String sql = "select a.rx_order_no_nvarchar,a.order_no_nvarchar,b.name_nvarchar,b.sex_nvarchar,b.age_int,c.drug_info_nvarchar,"
                +" a.create_time_datetime,a.dialectical_nvarchar,a.z_dialectical_nvarchar,a.z_dialectics_nvarchar,"
                +"a.chk_status_tinyint,a.failure_reason_nvarchar,a.failure_remark_nvarchar,a.d_type_tinyint"
                +" from order_rx_main a"
                +" inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                +" inner join order_rx_info c on a.rx_order_no_nvarchar = c.rx_order_no_nvarchar"
                +" inner join order_main d on a.order_no_nvarchar = d.order_no_nvarchar"
                +" where a.d_type_tinyint in (${DType})";

                if(UID != 0)
                    sql += " and d.dc_id_bigint = #{UID}";
                if(Pid != 0)
                    sql += " and d.pid_bigint = #{Pid}";
                sql += " order by a.create_time_datetime desc"
                +" limit #{skip},#{limit}";
                return sql;
            }

            public String RxDrugListSQL(@Param("RxOrderNOs") String RxOrderNOs) {
                String sql = "select rx_order_no_nvarchar,drug_name_nvarchar,spec_nvarchar,num_int,freq_nvarchar,med_way_nvarchar,remark_nvarchar,day_int from order_rx_drugs where rx_order_no_nvarchar in (${RxOrderNOs})";
                return sql;
            }

            public String PcRxListSQL(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType, 
                @Param("chkStatus") int chkStatus, @Param("name") String name, 
                @Param("skip") int skip, @Param("limit") int limit) {
                String sql = "with paget as (" + " select a.rx_order_no_nvarchar from order_rx_main a"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join order_patients_info e on b.patients_info_id_bigint = e.id_bigint" + " where b.dc_id_bigint = #{dcid}";
                if(beg != null && end != null)
                    sql += " and a.create_time_datetime > #{beg} and a.create_time_datetime < #{end}";
                if (name != null  && name.length() > 0)
                    sql += " and e.name_nvarchar like CONCAT('%',#{name},'%')";
                if (dType != 0)
                    sql += " and a.d_type_tinyint = #{dType}";
                if (chkStatus != -1)
                    sql += " and a.chk_status_tinyint = #{chkStatus}";
                sql += " order by a.create_time_datetime desc" + " limit #{skip},#{limit})"
                        + " select a.rx_order_no_nvarchar as RxOrderNO,a.order_no_nvarchar as OrderNO,a.create_time_datetime as CreateTime,a.is_recipe_doc_ca_bit as docIsRecipe,"
                        + " e.name_nvarchar as Name,"
                        + " cast(a.d_type_tinyint as SIGNED INTEGER) as DType,cast(a.chk_status_tinyint as SIGNED INTEGER) as ChkStatus,"
                        + " a.chk_doctor_nvarchar as ChkDoctor,a.chk_time_datetime as ChkTime" + " from paget p"
                        + " inner join order_rx_main a on a.rx_order_no_nvarchar = p.rx_order_no_nvarchar"
                        + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                        + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
                        + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
                        + " inner join order_patients_info e on b.patients_info_id_bigint = e.id_bigint";
                return sql;
            }

            public String PcRxCountSQL(@Param("dcid") long dcid, @Param("beg") Date beg, @Param("end") Date end, @Param("dType") Byte dType, 
                @Param("chkStatus") int chkStatus, @Param("name") String name) {
                String sql = " select count(a.rx_order_no_nvarchar) from order_rx_main a"
                    + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join order_patients_info e on b.patients_info_id_bigint = e.id_bigint" + " where b.dc_id_bigint = #{dcid}";
                if(beg != null && end != null)
                    sql += " and a.create_time_datetime > #{beg} and a.create_time_datetime < #{end}";
                if (name != null  && name.length() > 0)
                    sql += " and e.name_nvarchar like CONCAT('%',#{name},'%')";
                if (dType != 0)
                    sql += " and a.d_type_tinyint = #{dType}";
                if (chkStatus != -1)
                    sql += " and a.chk_status_tinyint = #{chkStatus}";
                return sql;
            }
        }
}
